﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iRemoteMulti.Forms
{
    public partial class Identify : Form
    {
        public int current_index = 0;
        public String key = "";
        public List<byte> buffer = new List<byte>();
        public byte send_index = 0xD0;
        public int send_times = 0;
        public Boolean send_finish_flag = false;
        public int time_count = 0;

        public DialogResult rs = DialogResult.No;

        public Identify(String key, int current_index)
        {
            InitializeComponent();

            this.key = key;
            this.current_index = current_index;
            key_init();
            timer1.Enabled = true;
        }

        public void key_init()
        {
            char[] charList = this.key.ToCharArray();
            byte[] byteList = Encoding.Default.GetBytes(charList);
            this.buffer.AddRange(byteList);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (iRemote.key_checked[current_index] == 1) 
            {
                rs = DialogResult.Yes;
                this.timer1.Enabled = false;
                this.Close();
            }
            timer1.Interval = 333;
            if (!send_finish_flag){
                if (buffer.Count > 0)
                {
                    Byte[] send_arr = new Byte[6];
                    send_arr[0] = 0x05; //数据结构长度
                    send_arr[1] = 0xFB; //开始标志
                    send_arr[2] = 0xCF;//操作码
                    send_arr[3] = send_index; //操作数据高位
                    send_arr[4] = buffer[0]; //操作数据低位
                    send_arr[5] = 0xFE; //操作数据低位
                    send_index++;
                    buffer.RemoveAt(0);
                    iRemote.onlySendDataToPort(send_arr, current_index);
                    if (send_index > 0xDF && buffer.Count == 0)
                    {
                        //timer1.Enabled = false;
                        send_times++;
                        send_index = 0xD0;
                        send_finish_flag = true;
                    }
                }
            }
            else
            {
                iRemote.sendDataToPort(0xC6, -1, current_index);
                send_finish_flag = false;
                key_init();
                if (send_times >= 3)
                {
                    rs = DialogResult.No;
                    this.timer1.Enabled = false;
                    button2.Visible = true;
                    label1.Text = "The key is not right, please input the right key";
                    label2.Text = "Checked failed";
                }
                timer1.Interval = 1000;
            }
        }

        private void Identify_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = rs;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            rs = DialogResult.No;
            this.timer1.Enabled = false;
            this.Close();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                string end = "";
                int wait = time_count % 3;
                for (int i = 0; i <= wait; i++)
                {
                    end += ".";
                }
                label1.Text ="Checking the key, please wait several seconds"+ end;
                time_count++;
                if (send_times < 3)
                {
                    label2.Text = "Trying times : " + (send_times + 1);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            rs = DialogResult.No;
            this.timer1.Enabled = false;
            this.Close();
        }

    }
}
